package com.woniu.woniuproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.entity.*;
import com.woniu.woniuproject.mapper.*;
import com.woniu.woniuproject.vo.AddLooseLabelVo;
import com.woniu.woniuproject.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author 徐威
 * @create 2020/11/16
 * @since 1.0.0
 */
@Service
public class LooseLabelService {
    @Autowired
    LooseLabelMapper looseLabelMapper;
    @Autowired
    UserMapper userMapper;
    /**
     * 散标分页查询
     */
    @Autowired
    Debt_infoMapper debt_infoMapper;
    public PageInfo<LooseLabel> queryByAplay(Date startDate, Date endDate, Page page,int status){
        PageHelper.startPage(page.getNowPage(),page.getMaxPage());
        List<LooseLabel> looseLabels = looseLabelMapper.selectByAplay(startDate, endDate,status);
        PageInfo<LooseLabel> info = new PageInfo<>(looseLabels);
        return info;
    }

    /**
     * 散标审核，审核通过后自动生成债权表
     * @param status
     * @param labelid
     * @return
     */
    @Transactional
    public int madifyStatusByid(int status, int labelid) {
        int i = looseLabelMapper.updateStatusByid(status, labelid);
        if(status==4){
            looseLabelMapper.updateDate(new Date(),labelid);
        }
        if (i>0&&status==1){
            Debt_info debt_info= new Debt_info();
            LooseLabel looseLabel = looseLabelMapper.selectBykey(labelid);
            debt_info.setDebtorsid(userMapper.selectByPrimaryKey(looseLabel.getDeborid()).getIdcart());
            debt_info.setDebtorsname(looseLabel.getDebtorname());
            debt_info.setLoanpurpose(looseLabel.getDebtoruser());
            debt_info.setLoantype(looseLabel.getLoanmode());
            debt_info.setRepaymentstyle(looseLabel.getRepaymentmethod());
            debt_info.setBorrowerid(looseLabel.getDeborid());
            debt_info.setAvailablemoney(looseLabel.getDebtormoney());
            debt_info.setMatchedstatus("11403");
            debt_info.setDebttype("散标");
            debt_info.setStartdate(new Date());
            debt_info.setLoanperiod(looseLabel.getDebormoney().toString());
            debt_info.setDabtrate(looseLabel.getIncomerate());
              Long id=new   Date().getTime();
            looseLabel.setProjectnumber(id.toString());
            debt_info.setDebttypename(looseLabel.getProjectnumber());
            System.out.println(looseLabel.getProjectnumber());
           int ids= debt_infoMapper.insert(debt_info);
           looseLabel.setDateinfoid(debt_info.getId());
           looseLabelMapper.updateBykey(looseLabel);
        }
        return i;
    }

    public PageInfo<LooseLabel> queryByAplay(Date startDates, Date endDates, Page page) {
        PageHelper.startPage(page.getNowPage(),page.getMaxPage());
        List<LooseLabel> looseLabels = looseLabelMapper.selectByAplays(startDates, endDates);
        PageInfo<LooseLabel> info = new PageInfo<>(looseLabels);
        return info;
    }
    @Transactional
    public int addLooseLabel(AddLooseLabelVo addLooseLabelVo) {
        return looseLabelMapper.insert(addLooseLabelVo);
    }

    public int queryByLastId() {
        return looseLabelMapper.queryByLastId();
    }

    public Map<String,Object> queryOneBykey(int id) {
        Map<String,Object> map = new HashMap();
        LooseLabel looseLabel = looseLabelMapper.selectBykey(id);
        map.put("looseLabel",looseLabel);
        return map;
    }
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    Product_accountMapper product_accountMapper;
    @Transactional
    public int transaction(User user, int labelid, Double money, Account account) {
        LooseLabel looseLabel = looseLabelMapper.selectBykey(labelid);
        Debt_info debt_info = new Debt_info();
        debt_info.setDebtorsname(looseLabel.getDebtorname());
        debt_info.setLoanpurpose(looseLabel.getDebtoruser());
        debt_info.setLoantype(looseLabel.getLoanmode());
        debt_info.setLoanstartdate(new Date());
        debt_info.setLoanperiod(looseLabel.getDebormoney().toString());
        debt_info.setRepaymentstyle(looseLabel.getRepaymentmethod());
        debt_info.setDebtyearrate(looseLabel.getIncomerate()*365);
        debt_info.setDebttransferredmoney(money);
        debt_info.setCreditor(user.getRealname());
        debt_info.setMatchedmoney(money);
        debt_info.setBorrowerid(user.getId());
        debt_info.setDebtstatus("0");
        debt_info.setAvailableperiod(looseLabel.getPeriods().toString());
        debt_info.setMatchedstatus("11402");
        debt_info.setDebttype("散标");
        debt_info.setDebttypename(looseLabel.getProjectnumber());
        looseLabel.setTransferredamount(looseLabel.getTransferredamount()+money);
        account.setBalance(account.getBalance()-money);
        account.setFrozen(account.getFrozen()+money);
        account.setInverstmentw(account.getInverstmentw()+money);
        account.setAddcapitaltotal(account.getAddcapitaltotal()+money);
        Product_account product_account = new Product_account();
        product_account.setPuid(user.getId());
        product_account.setPamount(money);
        product_account.setPbegindate(new Date());
        product_account.setPproducttype("2");
        product_account.setPexpectedannualincome(looseLabel.getIncomerate()*100*365);
        product_account.setPfrozenmoney(money);
        product_account.setPproductid(looseLabel.getLabelid());
        product_account.setPserialno(looseLabel.getProjectnumber());
        product_account.setPproductname(looseLabel.getProjectnumber());
        product_account.setPdeadline(looseLabel.getPeriods().toString());
        product_accountMapper.insert(product_account);
        int insert = debt_infoMapper.insert(debt_info);
        int i = accountMapper.updateByPrimaryKey(account);
        int rew =  looseLabelMapper.updateTransferredamount(looseLabel);
        return rew;
    }
}
